package com.hnust.service.impl;

import com.hnust.dao.ModifyScoreDao;
import com.hnust.service.ModifyScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人：zl
 * 创建时间：2020/10/29
 * 描述：
 */
@Service("ModifyScoreService")
public class ModifyScoreServiceImpl implements ModifyScoreService {

    @Autowired
    private ModifyScoreDao modifyScoreDao;

    /**
     * // 获取试卷列表
     * @param studentId 老师id
     * @param testId 课程id
     * @param queTypeNum 当前第几页
     * @param queNum 课程id
     * @param score 当前第几页
     * @return List
     */
    @Override
    public List<Double> modifyScore(String studentId, String testId, int queTypeNum, int queNum, double score) {
        String testStuScore = modifyScoreDao.queryTestStuScore(studentId,testId);
        if(testStuScore==null){
//            不能直接返回null，列表为空时表示的是[]。null！=[]
            return new ArrayList<>();
        }
        //将各题型分数分开
        String[] typeScore=testStuScore.split("#");

        //将每个题型的各个小题分开
        int length1=typeScore.length;
        Double newTotalScore=0.0;
        List<Double> scoreList=new ArrayList<>();

        for(int i=0;i<length1;i++){
            String[] stuScore=typeScore[i].split("￥");
            System.out.println("stuScore"+stuScore);
            int length=stuScore.length;
            double sum=0;
//            计算各类题型的总分
            for(int j=0;j<length;j++){
                sum=sum+Double.valueOf(stuScore[j]);
            }
            System.out.println("sum="+sum);
            scoreList.add(sum);
            newTotalScore=newTotalScore+sum;
            System.out.println("totalScore="+newTotalScore);
        }

        String[] stuScore=typeScore[queTypeNum].split("￥");
        stuScore[queNum] = String.valueOf(score);
        int length2=stuScore.length;
        String newStuScore="";
        for(int i=0;i<length2;i++){
            if(i!=0) {
                newStuScore=newStuScore+"￥"+stuScore[i];
            } else {
                newStuScore=newStuScore+stuScore[i];
            }
        }
        System.out.println("该题型修改后的字符串"+newStuScore);
        String newTestStuScore="";
        for(int i=0;i<length1;i++){
            if(i!=queTypeNum && i!=0){
                newTestStuScore=newTestStuScore+"#"+typeScore[i];
            }
            else if(i!=queTypeNum && i==0){
                newTestStuScore=newTestStuScore+typeScore[i];
            }
            else if(i==queTypeNum && i!=0){
                newTestStuScore=newTestStuScore+"#"+newStuScore;
            }
            else if(i==queTypeNum && i==0){
                newTestStuScore=newTestStuScore+newStuScore;
            }
        }
        System.out.println("修改后的字符串"+newTestStuScore);
        scoreList.add(newTotalScore);

        modifyScoreDao.UpdateTestStuScore(studentId,testId, newTestStuScore,newTotalScore);
        return scoreList;


    }

}
